﻿using Microsoft.AspNetCore.Mvc;
using System.ComponentModel;
using WebAPI.Common;

namespace WebAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ParentController : ControllerBase
    {
        /// <summary>
        /// 当前登录的用户id
        /// </summary>
        /// <returns></returns>
        public Guid CurrentUserId
        {
            get
            {
                Guid _userId = Guid.Empty;
                if (HttpContext.User.Identity.IsAuthenticated)
                {
                    var claimList = HttpContext.User.Claims.ToList();
                    var userIdStr = claimList.FirstOrDefault(t => t.Type == ConstantDim.AccountIdClaimType)?.Value ?? "0";

                    Guid.TryParse(userIdStr, out _userId);
                }
                return _userId;
            }
        }

        /// <summary>
        /// 当前登录的用户id
        /// </summary>
        /// <returns></returns>
        public string LoginName
        {
            get
            {
                string _logiName = string.Empty;
                if (HttpContext.User.Identity.IsAuthenticated)
                {
                    var claimList = HttpContext.User.Claims.ToList();
                    _logiName = claimList.FirstOrDefault(t => t.Type == ConstantDim.ApiLoginNameClaimType)?.Value ?? "0";
                }
                return _logiName;
            }
        }

        /// <summary>
        /// 获取客户端请求ip
        /// </summary>
        public string GetClientRequestIp
        {
            get
            {
                string IP = string.Empty;
                if (HttpContext.Connection.RemoteIpAddress != null)
                {
                    IP = HttpContext.Connection.RemoteIpAddress.ToString();
                    if (!string.IsNullOrWhiteSpace(IP))
                    {
                        if (IP == "::1")
                            IP = "127.0.0.1";
                    }

                }
                return IP;
            }
        }
    }

}
